package xh.util;

import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.audio.mp3.MP3AudioHeader;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.id3.ID3v23Frame;
import xh.util.Global;

import java.io.File;
import java.io.RandomAccessFile;

/**
 * Created by xh on 2016/6/27.
 */
public class ChangeName {

    public static void main(String[] args) {

        for (File file : Global.musicStore.allMusicFiles()) {
            change(file);
        }
    }

    static void change(File f) {
        try {
            File mp3 = f;
//            File mp3 = new File("F:\\music\\246-1.mp3");

            MP3File file = (MP3File) AudioFileIO.read(mp3);
//            MP3AudioHeader audioHeader = (MP3AudioHeader)file.getAudioHeader();

//            String songName=file.getID3v2Tag().frameMap.get("TIT2").toString();
//            String singer=file.getID3v2Tag().frameMap.get("TPE1").toString();
//            String author=file.getID3v2Tag().frameMap.get("TALB").toString();

            Object c = ((ID3v23Frame)file.getID3v2Tag().frameMap.get("TIT2")).getBody();
//            String title = (new String(c.toString().getBytes("ISO-8859-1"), "utf-8"));
            String title = c.toString();

            System.out.println(title);

            title = (title.split("\"")[1]);

            File newFile = new File(mp3.getParent(), title + ".mp3");

            System.out.println(newFile.getAbsolutePath());

            System.out.println(mp3.renameTo(newFile));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

